import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import formInfo from '@ohos.app.form.formInfo';
import rpc from '@ohos.rpc';

function backgroundFun(data: rpc.MessageSequence): rpc.Parcelable {
  if (data === null) {
    console.info('[BackGroundAbility] backgroundFun data is null');
  }
  let paramsObj: Record<string, Object> = JSON.parse(data.readString());
  console.info('[BackGroundAbility] backgroundFun paramsObj=' + data.readString());
  console.info('[BackGroundAbility] backgroundFun msg=' + paramsObj.msg);
  return new FuncCallback()
}

class FuncCallback implements rpc.Parcelable {
  unmarshalling(dataIn: rpc.MessageSequence): boolean {
    return true;
  }

  marshalling(dataOut: rpc.MessageSequence): boolean {
    return true;
  }
}

// 后台运行Ability
export default class BackGroundAbility extends UIAbility {
  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    // 接收到了卡片传递的消息
    if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) {
      let params = JSON.parse(want.parameters.params);
      let cardId = want.parameters[formInfo.FormParam.IDENTITY_KEY];

      console.info("[BackGroundAbility] onCreate card action params=" +
      JSON.stringify(params) + ',cardId=' + cardId);

      console.info("[BackGroundAbility] params.method:" + params.method);
      if (params.method === 'backgroundFunc') { // call 跳转
        console.info("[BackGroundAbility] onCreate call function:" + params.method);
        try {
          // 监听call事件所需的方法
          this.callee.on('backgroundFunc', backgroundFun);
        } catch (error) {
          console.log('register failed with error. Cause: ' + JSON.stringify(error));
        }
      }
    }
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
    try {
      this.callee.off('backgroundFunc');
    } catch (error) {
      console.info('register failed with error. Cause: ' + JSON.stringify(error));
    }
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/BackgroundPage', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
